﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TJADRICD.Model;
namespace TJADRICD.CollaborativeDesignPlugin.Form
{
    public partial class ChangePwd : System.Windows.Forms.Form
    {
        private String signerName;
        public String SignerName
        {
            get { return signerName; }
            set { signerName = value; }
        }

        private AccountDao curAccountDao;
        public AccountDao CurAccountDao
        {
            get { return curAccountDao; }
            set { curAccountDao = value; }
        }

        private bool isChangeAuthPwd;
        public bool IsChangeAuthPwd
        {
            get { return isChangeAuthPwd; }
            set { isChangeAuthPwd = value; }
        }

        public ChangePwd()
        {
            InitializeComponent();
        }

        public ChangePwd(bool isCgAuthPwd, String signer)
        {
            this.IsChangeAuthPwd = isCgAuthPwd;
            this.SignerName = signer;
            this.CurAccountDao = new AccountDao();
            InitializeComponent();
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            Account account = null;
            if (IsChangeAuthPwd)
            {
                account = CurAccountDao.getAccountById(this.SignerName);
                if (account != null && oldPwd.Text.Equals(account.AuthPassword))
                {
                    if (newPwd.Text.Length > 0 && newPwd.Text.Equals(confirmPwd.Text))
                    {
                        account.AuthPassword = newPwd.Text;
                        CurAccountDao.updateAccount(account);
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("两次密码不同");
                    }
                }
                else
                {
                    MessageBox.Show("原密码错误");
                }
            } 
            else
            {
                account = CurAccountDao.getAccountById(signerName);
                if (account != null && account.Password.Equals(oldPwd.Text))
                {
                    if (newPwd.Text.Length > 0 && newPwd.Text.Equals(confirmPwd.Text))
                    {
                        account.Password = newPwd.Text;
                        CurAccountDao.updateAccount(account);
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("两次密码不同");
                    }
                }
                else
                {
                    MessageBox.Show("原密码错误");
                }
            }
           
        }
    }
}
